package com.admission.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.xml.sax.SAXException;

import com.admission.model.EnrollDetail;
import com.admission.service.EnrollDetailManager;
import com.admission.util.ArrayUtils;
import com.admission.util.FileUtils;
import com.admission.util.PageUtils;
import com.admission.util.StaticEnroll;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/base")
@Results({
		@Result(name = CrudActionSupport.SUCCESS, location = "/statichtml/${pageName}", type = "dispatcher"),
		@Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "dispatcher"),
		@Result(name = "entry", location = "/statichtml/${pageName}", type = "dispatcher") })
public class EnrollDetailAction extends CrudActionSupport<EnrollDetail> {

	private static final long serialVersionUID = 1L;
	private String id;
	private EnrollDetailManager enrollDetailManager;
	private EnrollDetail entity;
	private String pageName;
	private String year;// 搜索的年份
	private String province;// 搜索的省份
	private String type;// 搜索的科类
	private String pageNo;
	private StaticEnroll staticEnroll;
	private Page<EnrollDetail> page = new Page<EnrollDetail>(
			PageUtils.DEFAULT_PAGE_SIZE);

	private ArrayUtils arrayUtils;

	public EnrollDetail getModel() {
		return entity;
	}

	public String entry() throws IllegalArgumentException, IOException, SAXException, IllegalAccessException {
		// enrollDetailManager.getTitleList(page);
		pageName = "enroll-detail-entry-" + page.getPageNo() + ".html";
		if (!new File(ServletActionContext.getServletContext().getRealPath(
				"/statichtml")
				+ "/" + pageName).exists()) {
			staticEnroll.createHtmlForEnrollDetail();
		}
		
		return "entry";

	}

	// 异步查询访问人数
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String visitorCount() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		String r = "";
		path += "/other/visitCount.properties";
		File f = new File(path);
		if (f.exists()) {
			f.createNewFile();
		}
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		if (session.get(id) == null) {
			r = FileUtils.getProperties(path, id, "1", true);
			session.put(id, "1");
		} else {
			r = FileUtils.getProperties(path, id, "1", false);
		}

		Struts2Utils.renderText(r);
		return null;
	}

	public String getSelectOption() throws IOException {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		path += "/other/enroll.xml";
		File f = new File(path);
		if (!f.exists()) {
			Struts2Utils.renderText("0");
		} else {
			FileReader fr = new FileReader(f);// 创建文件输入流
			BufferedReader in = new BufferedReader(fr);// 包装文件输入流，可整行读取
			String line;
			String s = "";
			while ((line = in.readLine()) != null) {
				s += line;
			}
			Struts2Utils.renderText(s.replaceAll(" ", "").replaceAll("><",
					"> <"));
		}
		return null;
	}

	@Override
	public String list() throws Exception {

		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";

		}
		pageName = "enroll-detail-" + year + "-" + province + "-" + type + "-"
				+ pageNo + ".html";
		String path = ServletActionContext.getServletContext().getRealPath(
				"/statichtml")
				+ "/" + pageName;
		File f = new File(path);
		// 检查静态页面是否生成
		if (!new File(ServletActionContext.getServletContext().getRealPath(
				"/statichtml")
				+ "/" + "enroll-detail.html").exists()) {
			staticEnroll.createHtmlForEnrollDetail();
		}

		if (StringUtils.isEmpty(year) || StringUtils.isEmpty(province)
				|| StringUtils.isEmpty(type)) {

			pageName = "enroll-detail.html";
		} else if (!f.exists()) {
			pageName = "enroll-detail.html?msg=1";
		} else {
			pageName += "?year=" + year + "&&province=" + province + "&&type="
					+ type;
		}

		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return RELOAD;
	}

	@Override
	public String save() throws Exception {
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	// 招生计划详细页面
	public String detail() {
		entity = enrollDetailManager.getById(id);
		return "detail";
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new EnrollDetail();
		} else {
			entity = enrollDetailManager.getById(id);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	@Autowired
	public void setEnrollDetailManager(EnrollDetailManager enrollDetailManager) {
		this.enrollDetailManager = enrollDetailManager;
	}

	@Autowired
	public void setStaticEnroll(StaticEnroll staticEnroll) {
		this.staticEnroll = staticEnroll;
	}

	public Page<EnrollDetail> getPage() {
		return page;
	}

	public void setPage(Page<EnrollDetail> page) {
		this.page = page;
	}

	public String getYearOption() {
		String r = "";
		for (String s : enrollDetailManager.yearList()) {
			r += "<option value='" + s + "'>" + s + "</option>";
		}
		return r;
	}

	public String getProvinceOption() {
		String r = "";
		for (String s : enrollDetailManager.provinceList()) {
			r += "<option value='" + s + "'>"
					+ arrayUtils.getProvinceMap().get(s) + "</option>";
		}
		return r;
	}

	public String getTypeOption() {
		String r = "";
		for (String s : enrollDetailManager.typeList()) {
			r += "<option value='" + s + "'>" + arrayUtils.getType().get(s)
					+ "</option>";
		}
		return r;
	}

	@Autowired
	public void setArrayUtils(ArrayUtils arrayUtils) {
		this.arrayUtils = arrayUtils;
	}

}
